package com.sdksoft.readcard

import android.os.Build
import com.csht.local.ble.pages.ScanBleDevicesFragment
import com.csht.utils.HHTipUtils

/**
 * 蓝牙扫描
 */
class Ble2ScanActivity : BaseInitActivity() {

    private val TAG = "Ble2ScanActivity"

    override fun initView() {
        super.initView()
        setTitleText("搜索蓝牙设备")
        baseActivityBinding.scrollView.isSmoothScrollingEnabled = false
        baseActivityBinding.scrollView.isNestedScrollingEnabled = false
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(
                arrayOf(
                    "android.permission.BLUETOOTH",
                    "android.permission.BLUETOOTH_ADMIN",
                    "android.permission.ACCESS_FINE_LOCATION",
                    "android.permission.ACCESS_COARSE_LOCATION",
                    "android.permission.BLUETOOTH_SCAN"
                ), 100
            )
        } else {
            initBleScan()
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        when (requestCode) {
            100 -> {
                if (grantResults.isNotEmpty()) {
                    //权限已授予
                    initBleScan()
                } else {
                    //权限被拒绝
                    HHTipUtils.getInstance().showToast(this, "权限被拒绝")
                }
            }
        }
    }

    private fun initBleScan() {
        supportFragmentManager.beginTransaction().add(R.id.ll_content, ScanBleDevicesFragment())
            .commitNow()
    }

    override fun onDestroy() {
        mainHandler.removeCallbacksAndMessages(null)
        super.onDestroy()
    }
}